package org.jeecg.modules.demo.parent.service.impl;

import org.jeecg.modules.demo.parent.entity.DemoFieldDefValMain;
import org.jeecg.modules.demo.parent.entity.DemoFieldDefValSub;
import org.jeecg.modules.demo.parent.mapper.DemoFieldDefValSubMapper;
import org.jeecg.modules.demo.parent.mapper.DemoFieldDefValMainMapper;
import org.jeecg.modules.demo.parent.service.IDemoFieldDefValMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 示例：控件默认值（主表）
 * @Author: jeecg-boot
 * @Date:   2020-05-20
 * @Version: V1.0
 */
@Service
public class DemoFieldDefValMainServiceImpl extends ServiceImpl<DemoFieldDefValMainMapper, DemoFieldDefValMain> implements IDemoFieldDefValMainService {

	@Autowired
	private DemoFieldDefValMainMapper demoFieldDefValMainMapper;
	@Autowired
	private DemoFieldDefValSubMapper demoFieldDefValSubMapper;


	@Override
	@Transactional
	public void saveMain(DemoFieldDefValMain demoFieldDefValMain, List<DemoFieldDefValSub> demoFieldDefValSubList) {
		demoFieldDefValMainMapper.insert(demoFieldDefValMain);
		if(demoFieldDefValSubList!=null && demoFieldDefValSubList.size()>0) {
			for(DemoFieldDefValSub entity:demoFieldDefValSubList) {
				//外键设置
				entity.setMainId(demoFieldDefValMain.getId());
				demoFieldDefValSubMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(DemoFieldDefValMain demoFieldDefValMain,List<DemoFieldDefValSub> demoFieldDefValSubList) {
		demoFieldDefValMainMapper.updateById(demoFieldDefValMain);
		
		//1.先删除子表数据
		demoFieldDefValSubMapper.deleteByMainId(demoFieldDefValMain.getId());
		
		//2.子表数据重新插入
		if(demoFieldDefValSubList!=null && demoFieldDefValSubList.size()>0) {
			for(DemoFieldDefValSub entity:demoFieldDefValSubList) {
				//外键设置
				entity.setMainId(demoFieldDefValMain.getId());
				demoFieldDefValSubMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		demoFieldDefValSubMapper.deleteByMainId(id);
		demoFieldDefValMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			demoFieldDefValSubMapper.deleteByMainId(id.toString());
			demoFieldDefValMainMapper.deleteById(id);
		}
	}
	
}
